package com.jh.concurrent.base.chain;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author JH_Wong
 * @projectName newforward
 * @title PrintProcesser.java
 * @package com.jh.concurrent.base.chain
 * @description 打印
 * @date 2019-05-08 23:09
 */
public class PrintProcesser extends Thread implements IFactoryProcesser{

    private LinkedBlockingQueue<Request> requests = new LinkedBlockingQueue<Request>();

    private IFactoryProcesser next;

    private volatile boolean isFinish = false;

    public PrintProcesser(){}

    public PrintProcesser(IFactoryProcesser next) {
        this.next = next;
    }

    @Override
    public void run() {
        while (!isFinish) {
            try {
                Request request = requests.take();
                System.out.println("Print：" + request);
                if (null != next) {
                    next.process(request);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void process(Request request) {
        requests.add(request);
    }
}
